"""
线程间通信-Queue

通过Queue的方式进行线程间同步
"""

import time
import threading
from queue import Queue

queue = Queue(maxsize=1000)


class GetDetailHtml(threading.Thread):
    """
    通过继承Thread类来实现多线程（复杂逻辑推荐使用）
    """

    def __init__(self, name):
        super().__init__(name=name)

    def run(self):
        while True:
            # 阻塞，线程安全（双端队列deque实现的）
            url = queue.get()

            print("get detail html started")
            time.sleep(2)
            print("get detail html end")


class GetDetailUrl(threading.Thread):
    def __init__(self, name):
        super().__init__(name=name)

    def run(self):
        while True:
            print("get detail url started")
            time.sleep(2)

            for i in range(20):
                queue.put("http://projectsedu.com/{id}".format(id=i))

            print("get detail url end")


def use_thread_extend():
    t1 = GetDetailHtml(name="GetDetailHtml")

    t_list = []
    for i in range(10):
        t2 = GetDetailUrl(name="GetDetailUrl")
        t_list.append(t2)

    t1.start()
    for t in t_list:
        t.start()

    # 线程阻塞
    t1.join()
    for t in t_list:
        t.join()


if __name__ == '__main__':
    use_thread_extend()
